package cn.com.zal.cgweb.pojo;

import java.util.Date;
import java.util.List;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A data access object (DAO) providing persistence and search support for
 * TcProduct entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see cn.com.zal.cgweb.pojo.TcProduct
 * @author MyEclipse Persistence Tools
 */

public class TcProductDAO extends BaseHibernateDAO {
	private static final Logger log = LoggerFactory
			.getLogger(TcProductDAO.class);
	// property constants
	public static final String SKU = "sku";
	public static final String CLASS_ID = "classId";
	public static final String TITLE = "title";
	public static final String PRICE = "price";
	public static final String QUANTITY = "quantity";
	public static final String DESCRIPTION = "description";
	public static final String ICO = "ico";
	public static final String PIC = "pic";
	public static final String UNIT = "unit";
	public static final String COLOR = "color";
	public static final String USER_ID = "userId";
	public static final String STATE = "state";
	public static final String REMARK = "remark";

	public void save(TcProduct transientInstance) {
		log.debug("saving TcProduct instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(TcProduct persistentInstance) {
		log.debug("deleting TcProduct instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public TcProduct findById(java.lang.Long id) {
		log.debug("getting TcProduct instance with id: " + id);
		try {
			TcProduct instance = (TcProduct) getSession().get(
					"cn.com.zal.cgweb.pojo.TcProduct", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(TcProduct instance) {
		log.debug("finding TcProduct instance by example");
		try {
			List results = getSession().createCriteria(
					"cn.com.zal.cgweb.pojo.TcProduct").add(
					Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding TcProduct instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from TcProduct as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByClassId(Object classId) {
		return findByProperty(CLASS_ID, classId);
	}

	public List findByTitle(Object title) {
		return findByProperty(TITLE, title);
	}

	public List findByPrice(Object price) {
		return findByProperty(PRICE, price);
	}

	public List findByQuantity(Object quantity) {
		return findByProperty(QUANTITY, quantity);
	}

	public List findByDescription(Object description) {
		return findByProperty(DESCRIPTION, description);
	}

	public List findByIco(Object ico) {
		return findByProperty(ICO, ico);
	}

	public List findByPic(Object pic) {
		return findByProperty(PIC, pic);
	}

	public List findByUnit(Object unit) {
		return findByProperty(UNIT, unit);
	}

	public List findByColor(Object color) {
		return findByProperty(COLOR, color);
	}

	public List findByUserId(Object userId) {
		return findByProperty(USER_ID, userId);
	}

	public List findByState(Object state) {
		return findByProperty(STATE, state);
	}

	public List findByRemark(Object remark) {
		return findByProperty(REMARK, remark);
	}
	public List findBySku(Object sku) {
		return findByProperty(SKU, sku);
	}
	

	public List findAll() {
		log.debug("finding all TcProduct instances");
		try {
			String queryString = "from TcProduct";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public TcProduct merge(TcProduct detachedInstance) {
		log.debug("merging TcProduct instance");
		try {
			TcProduct result = (TcProduct) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(TcProduct instance) {
		log.debug("attaching dirty TcProduct instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(TcProduct instance) {
		log.debug("attaching clean TcProduct instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}